package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.LogisticsStatusData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2017/12/22 16:37
 */

public class LogisticsAdapter extends RecyclerView.Adapter<LogisticsAdapter.MyViewHolder> {


    private Context mContext;
    private List<LogisticsStatusData> logisticsData;

    public LogisticsAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setLogisticsData(List<LogisticsStatusData> logisticsData) {
        this.logisticsData = logisticsData;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_logistics_layout, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        LogisticsStatusData loc = logisticsData.get(position);
        holder.tv_date.setText(loc.time);
        holder.tv_desc.setText(loc.status);
        if (position == 0) {
            holder.tv_date.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
            holder.tv_desc.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
            holder.tv_dot.setBackgroundResource(R.drawable.logistics_dot_choiced);
            holder.tv_line_top.setVisibility(View.INVISIBLE);
        } else if (position == logisticsData.size() - 1) {
            //底部数据
            holder.tv_line_bottom.setVisibility(View.INVISIBLE);
        } else {
            //设置圆点背景
            holder.tv_dot.setBackgroundResource(R.drawable.logistics_dot_normal);
        }
    }

    @Override
    public int getItemCount() {
        if (logisticsData == null)
            return 0;
        else
            return logisticsData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_date)
        TextView tv_date; //日期
        @BindView(R.id.tv_desc)
        TextView tv_desc; //描述
        @BindView(R.id.tv_line_top)
        TextView tv_line_top; //顶部线
        @BindView(R.id.tv_line_bottom)
        TextView tv_line_bottom;//底部线
        @BindView(R.id.tv_dot)
        TextView tv_dot; //圆点

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
